home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / Xprof / xprof / gc.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  14KB  |  457 lines

  1. /*==================================================================
  2.  *      File :          gc.c
  3.  *      Package:        Xprof
  4.  *
  5.  *      Author :        Aloke Gupta.
  6.  *
  7.  *  (C) Copyright 1992, Aloke Gupta.
  8.  *==================================================================*/
  9.  
  10. /*
  11.  * XprofGCvalues *create_gcval(int gcid)
  12.  * XprofGCvalues *get_gcval(int gcid)
  13.  * free_gcval(int gcid)
  14.  * copy_gcval(long src_id, long dst_id, int gcmask)$/
  15.  * fill_gcmask(char *ptr, int *gcmask) 
  16.  * fill_gcval  (FILE *fp, int valmask, XprofGCvalues *gcval) 
  17.  * gc_gfxindex (FILE *fp, int req_num, XprofGCvalues *gcval)
  18.  * gc_fontindex(FILE *fp, int req_num, XprofGCvalues *gcval)
  19.  * FontVal *create_fontval(long fontid)
  20.  * FontVal *get_fontval(long fontid)
  21.  * free_fontval(long fontid)
  22.  */
  23.  
  24. #include <stdio.h>
  25. #include "common.h"
  26. #include "profile.h"
  27.  
  28. char*        db_font_name();
  29.  
  30. XprofGCvalues    *create_gcval();
  31. XprofGCvalues    *get_gcval();
  32. FontVal     *create_fontval();
  33. FontVal     *get_fontval();
  34.  
  35. extern MsgType RequestType[];   /* Defined in table.c */
  36.  
  37. static AttributesTable GxmodeTable[]={
  38. /*
  39.  * index ,        xprof_name,     xscope_name,    where,     value
  40.  */
  41.    GXCLEAR,       "GXclear",      "Clear",        GXCOPY,      -1,
  42.    GXAND,         "GXand",        "And",          GXXOR,       -1,
  43.    GXANDREVERSE,  "GXandReverse", "AndReverse",   GXXOR,       -1,
  44.    GXCOPY,        "GXcopy",       "Copy",         GXCOPY,       0,   /* <-- */
  45.    GXANDINVERTED, "GXandInverted","AndInverted", GXXOR,       -1,
  46.    GXNOOP,        "GXnoop",        "Noop",        GXCOPY,      -1,
  47.    GXXOR,         "GXxor",         "Xor",         GXXOR,        1,   /* <-- */
  48.    GXOR,          "GXor",          "Or",          GXOR,        -1,
  49.    GXNOR,         "GXnor",         "Nor",         GXXOR,       -1,
  50.    GXEQUIV,       "GXequiv",       "Equiv",       GXXOR,       -1,
  51.    GXINVERT,      "GXinvert",      "Invert",      GXCOPY,      -1,
  52.    GXORREVERSE,   "GXorReverse",   "OrReverse",   GXXOR,       -1,
  53.    GXCOPYINVERTED,"GXcopyInverted","CopyInverted",GXCOPY,      -1,
  54.    GXORINVERTED,  "GXorInverted",  "OrInverted",  GXXOR,       -1,
  55.    GXNAND,        "GXnand",        "Nand",        GXXOR,       -1,
  56.    GXSET,         "GXset",         "Set",         GXCOPY,      -1,
  57.  };/* GxmodeTable */
  58.  
  59. unsigned int MAXGXMODES = sizeof(GxmodeTable) / sizeof(AttributesTable);
  60.  
  61. static AttributesTable LineStyleTable[] = {
  62. /*
  63.  * index ,         xprof_name,       xscope_name,    where,     value
  64.  */
  65.    LINESOLID,      "LineSolid",      "Solid",      LINESOLID,       0,/* <-- */
  66.    LINEONOFFDASH,  "LineOnOffDash",  "OnOffDash",  LINEDOUBLEDASH, -1,
  67.    LINEDOUBLEDASH, "LineDoubleDash", "DoubleDash", LINEDOUBLEDASH,  1,/* <-- */
  68. }; /*LineStyleTable */
  69.  
  70. unsigned int MAXLINESTYLES = sizeof(LineStyleTable) / sizeof(AttributesTable);
  71.  
  72. static AttributesTable FillStyleTable[] = {
  73. /*
  74.  * index ,          xprof_name,          xscope_name,     where,          value
  75.  */
  76.  FILLSOLID,         "FillSolid",         "Solid",         FILLSOLID,         0,
  77.  FILLOPAQUESTIPPLED,"FillOpaqueStippled","OpaqueStippled",FILLOPAQUESTIPPLED,1,
  78.  FILLSTIPPLED,      "FillStippled",      "Stippled",     FILLOPAQUESTIPPLED,-1,
  79.  FILLTILED,         "FillTiled",         "Tiled",        FILLOPAQUESTIPPLED,-1,
  80. }; /* FillStyleTable */
  81.  
  82. unsigned int MAXFILLSTYLES = sizeof(FillStyleTable) / sizeof(AttributesTable);
  83.  
  84. /* GC maintenance routines */
  85. static XprofGCvalues *gclist_head = NULL;
  86.  
  87. XprofGCvalues *create_gcval(gcid)
  88. long gcid;
  89. {
  90.     XprofGCvalues *gcval, *gcptr;
  91.  
  92.     /* Allocate a gcval with all elements zero */
  93.     gcval = (XprofGCvalues *) calloc(1, sizeof(XprofGCvalues));
  94.     if (gclist_head == NULL)    /* Empty list ? Insert at the head */
  95.     gclist_head = gcval;
  96.     else {            /* Attach it at the end of the list */
  97.     gcptr = gclist_head;
  98.     while(gcptr->next != NULL)
  99.         gcptr = gcptr->next;
  100.     gcptr->next = gcval;
  101.     gcval->next = NULL;
  102.     }
  103.     gcval->id = gcid;
  104.     return gcval;
  105. }
  106.  
  107. XprofGCvalues *get_gcval(gcid)
  108. long gcid;
  109. {
  110.     XprofGCvalues *gcptr;
  111.  
  112.     gcptr = gclist_head;
  113.     while( (gcptr != NULL ) && (gcptr->id != gcid) ) 
  114.     gcptr = gcptr->next;
  115.     if (gcptr == NULL)     /* Cannot find this gcval !! */
  116.     fprintf(stderr, "Line %d: Cannot find GC number %lx\n", _LINE_NUM,gcid);
  117.  
  118.     return(gcptr);
  119. }
  120.  
  121. free_gcval(gcid)
  122. long gcid;
  123. {
  124.     XprofGCvalues *previous, *current;
  125.  
  126.     current = gclist_head;
  127.     /* Search for this GC */
  128.     while((current !=NULL) && (current->id != gcid)) {
  129.     previous = current;
  130.     current  = current->next;
  131.     if (current == NULL) {     /* Cannot find this gcval !! */
  132.         fprintf(stderr, "Line %d: Cannot find GC number %lx\n", 
  133.             _LINE_NUM,gcid);
  134.         return;
  135.     }
  136.     }
  137.     if (current == gclist_head)     /* Only one entry in this list !! */
  138.     gclist_head = NULL;        /* Empty the list */
  139.     else previous->next = current->next;/* Remove this from the list */
  140.     free(current);
  141. }
  142.  
  143. copy_gcval(src_id, dst_id, gcmask)
  144. long src_id, dst_id;
  145. int gcmask;
  146. {
  147.     XprofGCvalues *src_gcval, *dst_gcval;
  148.     
  149.     src_gcval = get_gcval(src_id);
  150.     dst_gcval = get_gcval(dst_id);
  151.     if ((src_gcval == NULL) || (dst_gcval == NULL))
  152.     return;
  153.     /*
  154.      * Now modify the appropriate values
  155.      */
  156.     if (gcmask & GCFunction)
  157.     dst_gcval->function   = src_gcval->function;
  158.     if (gcmask & GCLineWidth)
  159.     dst_gcval->line_width = src_gcval->line_width;
  160.     if (gcmask & GCLineStyle)
  161.     dst_gcval->line_style = src_gcval->line_style;
  162.     if (gcmask & GCFillStyle)
  163.     dst_gcval->fill_style = src_gcval->fill_style;
  164. }
  165.  
  166. fill_gcmask(ptr, gcmask) 
  167. char *ptr;
  168. int  *gcmask;
  169. {
  170.     while ( isspace(*ptr)) ptr++;    /* Eat leading white space */
  171.     while (!isspace(*ptr)) ptr++;    /* Eat the token */
  172.     while ( isspace(*ptr)) ptr++;    /* Eat leading white space */
  173.  
  174.     if (t_search(ptr, "<ALL>") != 0)
  175.     *gcmask = ~0;
  176.     else if (t_search(ptr, "0") !=0) {
  177.     *gcmask = 0;
  178.     }
  179.     else while (*ptr) {
  180.     if (t_search(ptr, "function") != 0)
  181.         *gcmask |= GCFunction;
  182.     else if (t_search(ptr, "line-width") != 0)
  183.         *gcmask |= GCLineWidth;
  184.     else if (t_search(ptr, "line-style") != 0)
  185.         *gcmask |= GCLineStyle;
  186.     else if (t_search(ptr, "fill-style") != 0)
  187.         *gcmask |= GCFillStyle;
  188.     else if (t_search(ptr, "font") != 0)
  189.         *gcmask |= GCFont;
  190.     
  191.     while (!isspace(*ptr)) ptr++;       /* Eat the token */
  192.     while ( isspace(*ptr) || (*ptr == '|') ) /* Next gc value */
  193.         ptr++; 
  194.     }
  195. }
  196.  
  197. fill_gcval(fp, valmask, gcval) 
  198. FILE *fp;
  199. int  valmask;
  200. XprofGCvalues *gcval;
  201. {
  202.     char *ptr;
  203.     char sbuf1[132], sbuf2[132]; /* Temp. slots for sscanf*/
  204.     char in_string[MAXSTRINGSIZE];
  205.  
  206.     while(valmask) {
  207.     if (fgets(in_string,MAXSTRINGSIZE,fp) == NULL) {
  208.             return;
  209.     }
  210.     _LINE_NUM++;
  211.     ptr = in_string;
  212.  
  213.     while (isspace(*ptr)) ptr++;    /* Remove leading white space */
  214.     /* Entry for function ? */
  215.     if (t_search(ptr, "function:") != 0) {
  216.         valmask &= ~GCFunction;   /* function has been found */
  217.         sscanf(ptr, "%s %s", sbuf1, sbuf2);
  218.  
  219.         /* Which graphics function is it ? */
  220.         if      (t_search(sbuf2, "Clear") != 0)
  221.         gcval->function =    GXclear;
  222.         else if (t_search(sbuf2, "And") != 0)
  223.         gcval->function =    GXand;
  224.         else if (t_search(sbuf2, "AndReverse") != 0)
  225.         gcval->function =    GXandReverse;
  226.         else if (t_search(sbuf2, "Copy") != 0)
  227.         gcval->function =    GXcopy;
  228.         else if (t_search(sbuf2, "AndInverted") != 0)
  229.         gcval->function =    GXandInverted;
  230.         else if (t_search(sbuf2, "Noop") != 0)
  231.         gcval->function =    GXnoop;
  232.         else if (t_search(sbuf2, "Xor") != 0)
  233.         gcval->function =    GXxor;
  234.         else if (t_search(sbuf2, "Or") != 0)
  235.         gcval->function =    GXor;
  236.         else if (t_search(sbuf2, "Nor") != 0)
  237.         gcval->function =    GXnor;
  238.         else if (t_search(sbuf2, "Equiv") != 0)
  239.         gcval->function =    GXequiv;
  240.         else if (t_search(sbuf2, "Invert") != 0)
  241.         gcval->function =    GXinvert;
  242.         else if (t_search(sbuf2, "OrReverse") != 0)
  243.         gcval->function =    GXorReverse;
  244.         else if (t_search(sbuf2, "CopyInverted") != 0)
  245.         gcval->function =    GXcopyInverted;
  246.         else if (t_search(sbuf2, "OrInverted") != 0)
  247.         gcval->function =    GXorInverted;
  248.         else if (t_search(sbuf2, "Nand") != 0)
  249.         gcval->function =    GXnand;
  250.         else if (t_search(sbuf2, "Set") != 0)
  251.         gcval->function =    GXset;
  252.     }
  253.  
  254.     /* Entry for line width ? */
  255.     else if (t_search(ptr, "line-width:") != 0) {
  256.         valmask &= ~GCLineWidth;   /* linewidth has been found */
  257.         sscanf(ptr, "%s %d", sbuf1, &(gcval->line_width));
  258.     }
  259.  
  260.     /* Entry for line style ? */
  261.     else if (t_search(ptr, "line-style:") != 0) {
  262.         valmask &= ~GCLineStyle;   /* linestyle has been found */
  263.         sscanf(ptr, "%s %s", sbuf1, sbuf2);
  264.         /* Which line style is it ? */
  265.         if      (t_search(sbuf2, "Solid") != 0)
  266.         gcval->line_style = LineSolid;
  267.         else if (t_search(sbuf2, "OnOffDash") != 0)
  268.         gcval->line_style = LineOnOffDash;
  269.         else if (t_search(sbuf2, "DoubleDash") != 0)
  270.         gcval->line_style = LineDoubleDash;
  271.     }
  272.  
  273.     /* Entry for fill style ? */
  274.     else if (t_search(ptr, "fill-style:") != 0) {
  275.         valmask &= ~GCFillStyle;   /* fillstyle has been found */
  276.         sscanf(ptr, "%s %s", sbuf1, sbuf2);
  277.  
  278.         /* Which fill style is it ? */
  279.         if      (t_search(sbuf2, "Solid") != 0)
  280.         gcval->fill_style = FillSolid;
  281.         else if (t_search(sbuf2, "Tiled") != 0)
  282.         gcval->fill_style = FillTiled;
  283.         else if (t_search(sbuf2, "Stippled") != 0)
  284.         gcval->fill_style = FillStippled;
  285.         else if (t_search(sbuf2, "OpaqueStippled") != 0)
  286.         gcval->fill_style = FillOpaqueStippled;
  287.     }
  288.     else if (t_search(ptr, "font:") != 0) {
  289.         long fontid;
  290.         valmask &= ~GCFont;   /* font has been found */
  291.         sscanf(ptr, "%s %s %lx", sbuf1, sbuf2, &fontid);
  292.         gcval->fontval = get_fontval(fontid);
  293.     }
  294.     }
  295. }
  296.  
  297. gc_gfxindex(fp, req_num, gcval)
  298. FILE *fp;
  299. int req_num;
  300. XprofGCvalues *gcval;
  301. {
  302.     int gxmode=0,linestyle=0, fillstyle=0, linewidth=0;/* Respective indices */
  303.     int index;
  304.     char *req_name;
  305.  
  306.     req_name = RequestType[req_num].name;
  307.     /*
  308.      * Fill gxmode
  309.      */
  310.     index  = GxmodeTable[gcval->function].where;    /* Where is it ? */
  311.     gxmode = GxmodeTable[index].value;
  312.     if (gxmode == -1) {
  313.       fprintf(fp,
  314.         "Line %d -- Error: GxmodeTable has been incorrectly initialized\n",
  315.      _LINE_NUM);
  316.     exit(1);
  317.     }
  318.     else if (gxmode != GxmodeTable[gcval->function].value)
  319.       fprintf(fp, 
  320.     "Line %d -- ( %s ) Warning: Can't find gxmode    %-12s substituting %s\n",
  321.     _LINE_NUM, req_name,
  322.     GxmodeTable[gcval->function].xprof_name,
  323.     GxmodeTable[index].xprof_name);
  324.  
  325.     /*
  326.      * Fill linestyle
  327.      */
  328.     index     = LineStyleTable[gcval->line_style].where;   /* Where is it ? */
  329.     linestyle = LineStyleTable[index].value;
  330.     if (linestyle == -1) {
  331.       fprintf(fp,
  332.         "Line %d -- Error: LineStyleTable has been incorrectly initialized\n",
  333.      _LINE_NUM);
  334.     exit(1);
  335.     }
  336.     else if (linestyle != LineStyleTable[gcval->line_style].value)
  337.       fprintf(fp, 
  338.     "Line %d -- ( %s ) Warning: Can't find linestyle %-12s substituting %s\n",
  339.     _LINE_NUM, req_name,
  340.     LineStyleTable[gcval->line_style].xprof_name,
  341.     LineStyleTable[index].xprof_name);
  342.  
  343.     /*
  344.      * Fill fillstyle
  345.      */
  346.     index     = FillStyleTable[gcval->fill_style].where;   /* Where is it ? */
  347.     fillstyle = FillStyleTable[index].value;
  348.     if (fillstyle == -1) {
  349.       fprintf(fp,
  350.         "Line %d -- Error: FillStyleTable has been incorrectly initialized\n",
  351.      _LINE_NUM);
  352.     exit(1);
  353.     }
  354.     else if (fillstyle != FillStyleTable[gcval->fill_style].value)
  355.       fprintf(fp, 
  356.     "Line %d -- ( %s ) Warning: Can't find fillstyle %-12s substituting %s\n",
  357.     _LINE_NUM, req_name, 
  358.     FillStyleTable[gcval->fill_style].xprof_name,
  359.     FillStyleTable[index].xprof_name);
  360.  
  361.     /*
  362.      * Fill linewidth
  363.      */
  364.     /* Fetch the index from database created by parse.c */
  365.     linewidth = get_db_linewidth_index(gcval->line_width, NOINSTALL);
  366.     if (linewidth == -1) {
  367.       int tmp;
  368.       tmp = get_db_closest_lw(gcval->line_width);
  369.       fprintf(fp,
  370.     "Line %d -- ( %s ) Warning: Can't find linewidth %2d, substituting %2d\n",
  371.     _LINE_NUM, req_name, gcval->line_width, tmp);
  372.       linewidth = get_db_linewidth_index(tmp, NOINSTALL);
  373.     }
  374.     index = GFXINDEX(gxmode, linestyle, fillstyle, linewidth) ;
  375.     return (index);
  376. }
  377.  
  378. /* FontVal maintenance routines */
  379. static FontVal *fontval_list_head = NULL;
  380.  
  381. FontVal *create_fontval(fontid)
  382. long fontid;
  383. {
  384.     FontVal *fontval, *fvptr;
  385.  
  386.     /* Allocate a fontval with all elements zero */
  387.     fontval = (FontVal *) calloc(1, sizeof(FontVal));
  388.     if (fontval_list_head == NULL)    /* Empty list ? Insert at the head */
  389.     fontval_list_head = fontval;
  390.     else {            /* Attach it at the end of the list */
  391.     fvptr = fontval_list_head;
  392.     while(fvptr->next != NULL)
  393.         fvptr = fvptr->next;
  394.     fvptr->next = fontval;
  395.     fontval->next = NULL;
  396.     }
  397.     fontval->id = fontid;
  398.     return fontval;
  399. }
  400.  
  401. FontVal *get_fontval(fontid)
  402. long fontid;
  403. {
  404.     FontVal *fvptr;
  405.  
  406.     fvptr = fontval_list_head;
  407.     while( (fvptr != NULL) && (fvptr->id != fontid) )
  408.     fvptr = fvptr->next;
  409.     if (fvptr == NULL)     /* Cannot find this fontval !! */
  410.     fprintf(stderr, "Line %d: Cannot find Font number %lx\n", _LINE_NUM,fontid);
  411.  
  412.     return(fvptr);
  413. }
  414.  
  415. free_fontval(fontid)
  416. long fontid;
  417. {
  418.     FontVal *previous, *current;
  419.  
  420.     current = fontval_list_head;
  421.     /* Search for this Font */
  422.     while( (current != NULL) && (current->id != fontid) ) {
  423.     previous = current;
  424.     current  = current->next;
  425.     if (current == NULL) {     /* Cannot find this fontval !! */
  426.         fprintf(stderr, "Line %d: Cannot find Font number %lx\n", 
  427.             _LINE_NUM,fontid);
  428.         return;
  429.     }
  430.     }
  431.     if (current == fontval_list_head)     /* Only one entry in this list !! */
  432.     fontval_list_head = NULL;        /* Empty the list */
  433.     else previous->next = current->next;/* Remove this from the list */
  434.     if (current->fontname !=NULL)
  435.     free(current->fontname);
  436.     free(current);
  437. }
  438.  
  439. gc_fontindex(fp, req_num, gcval)
  440. FILE *fp;
  441. int req_num;
  442. XprofGCvalues *gcval;
  443. {
  444.     int index=0;
  445.  
  446.     index=get_db_fontname_index(gcval->fontval->fontname, NOINSTALL);
  447.     if (index == -1) {
  448.     index = 0;
  449.         fprintf(fp,
  450.         "Line %d -- ( %s ) Warning: Can't find font %s substituting %s\n",
  451.       _LINE_NUM, RequestType[req_num].name,
  452.       gcval->fontval->fontname, db_font_name(index));
  453.  
  454.     }
  455.     return(index);
  456. }
  457.